/*
 * 文件名：AddressServiceImpl.java
 * 版权：
 * 描述：TODO
 * 修改人：zuo liubo
 * 修改时间：2013-2-2 下午5:04:32
 */
package com.scpii.api.service.ec.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.api.common.exception.ClientException;
import com.scpii.api.domain.ec.Address;
import com.scpii.api.persistence.ec.AddressMapper;
import com.scpii.api.service.ec.AddressService;

/**
 * @description TODO
 * @author zuo liubo
 * @date 2013-2-2
 */
@Service("addressService")
public class AddressServiceImpl implements AddressService {

	@Resource
	private AddressMapper addressMapper;
	private int affectRow = 0;

	@Override
	public Long insertAddress(Address address) {
		Long id = addressMapper.generateId();
		address.setAddressId(id);
		affectRow = addressMapper.insert(address);
		if (affectRow == 0) {
			throw new ClientException("insert address error");
		}
		return id;
	}

	@Override
	public List<Address> getAddressByUserId(Long userId) {
		return addressMapper.getAddressByUserId(userId);
	}

	@Override
	public Long updateAddress(Address address) {
		affectRow = addressMapper.updateAddressById(address);
		if (affectRow == 0) {
			throw new ClientException("update address error");
		}
		return address.getAddressId();
	}

	@Override
	public boolean deleteAddressById(long id) {
		affectRow = addressMapper.deleteAddressById(id);
		return new Boolean(affectRow == 1);
	}

	@Override
	public List<Address> selectByTargetId(Long targetId) {
		return addressMapper.selectByTargetId(targetId);
	}

}
